package servlet;

import com.pml.factory.DAOFactory;
import com.pml.vo.User;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String path="login.jsp";
        List<String> info = new ArrayList<>();
        String userId = req.getParameter("userId");
        String password = req.getParameter("password");
        if (userId == null || "".equals(userId)) {
            info.add("用户名不能为空");
        }
        if (password == null || "".equals(password)) {
            info.add("密码不能为空");
        }
        if (info.size() == 0) {

            User user = new User();
            user.setUserId(userId);
            user.setPassword(password);
            try {
                if (DAOFactory.getUserDAOInstance().findUser(user)) {

                    info.add("登陆成功,欢迎" + user.getName());
                } else {
                    info.add("登录失败");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        req.setCharacterEncoding("utf-8");
        req.setAttribute("info",info);
        req.getRequestDispatcher(path).forward(req,resp);


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
